home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 4448 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  79 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: mozart.unx.sas.com!walker
  3. From: walker@chang.unx.sas.com (Doug Walker)
  4. Subject: Re: 3 bugs in SAS/C v. 6.56
  5. Originator: walker@chang.unx.sas.com
  6. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  7. Message-ID: <DnJG80.st@unx.sas.com>
  8. Date: Thu, 29 Feb 1996 13:02:24 GMT
  9. X-Nntp-Posting-Host: chang.unx.sas.com
  10. References: <4gjvvc$a3t@gjallar.daimi.aau.dk> <312EFB06.708D@diku.dk>
  11. Organization: SAS Institute Inc.
  12.  
  13.  
  14. In article <312EFB06.708D@diku.dk>, Morten B=?iso-8859-2?Q?=F8geskov Jensen <bogeskov@diku.dk>?= writes:
  15. |> However I belive that ">" and other boolean operations are
  16. |> left-associative (I know), as so they take the type of the 2nd (right)
  17. |> parameter, and since 0x7fff per default is signed, there is an implicit
  18. |> type conversion of unsigned short to signed short. this is most
  19. |> unfortunate, however this is the way most C-compilers work.
  20.  
  21. This is not correct.  Binary operators in C do not simply take the type
  22. of the right parameter.  The reality is a little more complicated.
  23.  
  24. To start off with, both operands are subjected to the "usual arithmetic
  25. conversions", described in section 3.2.1.5 of the ANSI standard.  These
  26. basically boil down to the fact that operands of binary operators are
  27. promoted to the "longer" of the two types involved, but they are always
  28. promoted to at least type "int".  Type "length" is basically
  29.  
  30.     long double
  31.     double
  32.     float
  33.     unsigned long
  34.     long/unsigned int
  35.     int
  36.  
  37. If an operand's type is char, unsigned char, signed char, short, or
  38. unsigned short, it will be promoted to the "shortest" item in the
  39. preceding list that will hold all the possible values associated 
  40. with its type.
  41.  
  42. Once both operands have been promoted according to the table, the types
  43. of each operand are examined.  If one is longer than the other,
  44. according to the table, the shorter operand is promoted to the longer
  45. type.
  46.  
  47. There is an ambiguity at long/unsigned int: if integers are four bytes, a
  48. long will not represent all possible values stored in an unsigned int, so
  49. if one parameter to an operation is long and the other unsigned int, both
  50. operands must be converted to unsigned long.  If integers are two bytes, 
  51. this is not a problem and the unsigned int is converted to long.
  52.  
  53. In this particular case, the two unsigned short parameters to the | operator 
  54. will be converted to int since long integers are being used.  This should be
  55. OK, since their values should be translated without sign-extending because 
  56. they are unsigned.  This should result in an integer with value 0x0000ffff.
  57. The comparison now takes an int on the left (result of the | operation) and
  58. an int on the right, so its result is int.
  59.  
  60. If integers were two bytes for this program instead of one, the unsigned shorts
  61. would get promoted to unsigned int, and the result of the | operation would be
  62. unsigned int.  The 0x7fff on the right of the > would be promoted to unsigned
  63. int, and the comparison should still work.
  64.  
  65. Your proposed fix, casting the right side to (unsigned short), does nothing in
  66. either case since the unsigned short parm would be immediately promoted to 
  67. either int or unsigned int, depending on the length of an int.
  68.  
  69.  
  70.  
  71. -- 
  72.   *****                    / walker@unx.sas.com
  73.  *|_o_o|\\     Doug Walker<  BIX, Portal: djwalker
  74.  *|. o.| ||                \ AOL: weissblau
  75.   | o  |//     
  76.   ====== 
  77. Any opinions expressed are mine, not those of SAS Institute, Inc.
  78.  
  79.